/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package iie.tools;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.QueryOperators;
import com.mongodb.ServerAddress;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author zwlin
 */
public class MongoHelper {

    static private Logger logger = Logger.getLogger(MongoHelper.class.getName());

    MongoClient conn;
    DB db;
    DBCollection collection;

    String fieldName;
    String[] retFieldNames;

    public MongoHelper(String[] hosts, String dbname, String tablename, String username, String passwd, String fieldName, String[] retFieldNames) throws Exception {
        this.fieldName = fieldName;
        this.retFieldNames = retFieldNames;
        try {
            List<ServerAddress> seeds = new ArrayList<ServerAddress>();
            for (String host : hosts) {
                String[] serv = host.split(":");
                if (serv != null && serv.length == 2) {
                    seeds.add(new ServerAddress(serv[0], Integer.parseInt(serv[1])));
                }
            }
            MongoCredential credential = MongoCredential.createScramSha1Credential(username, dbname, passwd.toCharArray());
            List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
            credentialsList.add(credential);

            conn = new MongoClient(seeds, credentialsList);
            db = conn.getDB(dbname);
            collection = db.getCollection(tablename);
        } catch (Exception ex) {
            try {
                conn.close();
            } catch (Exception ex1) {
            }
            throw new Exception("cacnnot init mongo helper !", ex);

        }
    }

    public List<Object[]> find(String[] value) {
        List<Object[]> rtnList = new LinkedList<>();
        long time = System.currentTimeMillis();
        logger.info("try to find " + value.length + " records from mongo.");
        DBCursor rslts = collection.find(new BasicDBObject(fieldName, new BasicDBObject(QueryOperators.IN, value)));
//		DBCursor rslts = collection.find(new BasicDBObject(fieldName, new BasicDBObject(QueryOperators.IN, ins)));
        while (rslts.hasNext()) {
            DBObject line = rslts.next();
            if (false) {
                logger.info(line);
            }
            Object[] rec = new Object[retFieldNames.length];
            int i = -1;
            for (String field : retFieldNames) {
                rec[++i] = line.get(field);
            }
            rtnList.add(rec);
        }
        logger.info("find from mongo cost time: " + (System.currentTimeMillis() - time) / 1000 + " s");
        return rtnList;
    }

}
